home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / tutorials / custEducation / opengl1 / examples / text / fineClip.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  5.3 KB  |  248 lines

  1. /*
  2.  * Copyright 1993, 1996, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17.  
  18. /* fineClip.c - render a bitmap font string using X Window System fonts; 
  19.  *    adjust the viewport so the text is not prematurely clipped.
  20.  *
  21.  *    Escape key     - exit the program
  22.  */
  23.  
  24. #include <GL/gl.h>
  25. #include <GL/glu.h>
  26. #include <GL/glut.h>
  27.  
  28. #include <stdio.h>
  29.  
  30. /*  Function Prototypes  */
  31.  
  32. GLvoid  initgfx( GLvoid );
  33. GLvoid  drawScene( GLvoid );
  34. GLvoid  animate( GLvoid );
  35. GLvoid  visibility( int );
  36. GLvoid  reshape( GLsizei, GLsizei );
  37. GLvoid  keyboard( GLubyte, GLint, GLint );
  38.  
  39. int getBitmapStringLength( void *, char * );
  40.  
  41. void printHelp( char * );
  42.  
  43. /* Global Definitions */
  44.  
  45. #define KEY_ESC    27    /* ascii value for the escape key */
  46.  
  47. /* Global Variables */
  48.      
  49. static GLdouble    left, right, bottom, top;
  50. static GLint       strLength;
  51. static void       *fixedFont, *strokeFont;
  52. static char        *charStr = "A text string.";
  53.  
  54. GLvoid
  55. main( int argc, char *argv[] )
  56. {
  57.     GLsizei width, height;
  58.  
  59.     glutInit( &argc, argv );
  60.  
  61.     width = glutGet( GLUT_SCREEN_WIDTH ); 
  62.     height = glutGet( GLUT_SCREEN_HEIGHT );
  63.     glutInitWindowPosition( (width / 2), height / 4 );
  64.     glutInitWindowSize( (width / 2) - 4, height / 2 );
  65.     glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH );
  66.     glutCreateWindow( argv[0] );
  67.  
  68.     initgfx();
  69.  
  70.     glutIdleFunc( animate );
  71.     glutVisibilityFunc( visibility );
  72.     glutKeyboardFunc( keyboard );
  73.     glutReshapeFunc( reshape );
  74.     glutDisplayFunc( drawScene ); 
  75.  
  76.     printHelp( argv[0] );
  77.  
  78.     glutMainLoop();
  79. }
  80.  
  81. void
  82. printHelp( char *progname )
  83. {
  84.     fprintf(stdout,
  85.         "\n%s - render a bitmap font string; adjust the viewport "
  86.         "so the text is\nnot prematurely clipped.\n\n"
  87.         "Escape Key        - exit the program\n\n",
  88.         progname);
  89. }
  90.  
  91. GLvoid
  92. initgfx( GLvoid )
  93. {
  94.     GLdouble    aspect;
  95.  
  96.     glClearColor( 0.0, 0.0, 0.0, 1.0 );
  97.     glShadeModel( GL_FLAT );
  98.  
  99.     /* Set up two fonts to use for rendering */
  100.     fixedFont = GLUT_BITMAP_TIMES_ROMAN_24;
  101.     strokeFont = GLUT_STROKE_ROMAN;
  102.  
  103.     strLength = getBitmapStringLength( fixedFont, charStr );
  104. }
  105.  
  106. GLvoid 
  107. keyboard( GLubyte key, GLint x, GLint y )
  108. {
  109.     switch (key) {
  110.     case KEY_ESC:    /* Exit whenever the Escape key is pressed */
  111.         exit(0);
  112.     }
  113. }
  114.  
  115. GLvoid
  116. reshape( GLsizei width, GLsizei height )
  117. {
  118.     GLdouble    aspect;
  119.  
  120.     glViewport( -strLength, 0, width + strLength, height );
  121.  
  122.     aspect = (GLdouble) (width + strLength) / (GLdouble) height;
  123.     if ( aspect < 1.0 ) {
  124.         left = -2.0;
  125.         right = 2.0;
  126.         bottom = -2.0 * ( 1.0 / aspect );
  127.         top = 2.0 * ( 1.0 / aspect );
  128.     } else {
  129.         left = -2.0 * aspect;
  130.         right = 2.0 * aspect;
  131.         bottom = -2.0;
  132.         top = 2.0;
  133.     }
  134.  
  135.     glMatrixMode( GL_PROJECTION );
  136.     glLoadIdentity();
  137.     glOrtho( left, right, bottom, top, -1.0, 1.0 );
  138.     glMatrixMode( GL_MODELVIEW );
  139. }
  140.  
  141. GLvoid 
  142. animate( GLvoid )
  143. {
  144.     /* Tell GLUT to redraw the scene */
  145.     glutPostRedisplay();
  146. }
  147.  
  148. GLvoid
  149. visibility( int state ) 
  150. {
  151.     if (state == GLUT_VISIBLE) {
  152.         glutIdleFunc( animate );
  153.     } else {
  154.         glutIdleFunc( NULL );
  155.     }
  156. }
  157.  
  158. GLvoid
  159. renderBitmapString( void *font, char *string )
  160. {
  161.         int i;
  162.         int len = (int) strlen(string);
  163.         for (i = 0; i < len; i++) {
  164.                 glutBitmapCharacter(font, string[i]);
  165.         }
  166. }
  167.  
  168. /* Compute the length of a bitmap string ( in pixels ) */
  169. int 
  170. getBitmapStringLength( void *font, char *string )
  171. {
  172.     int width = 0;
  173.  
  174. #if GLUT_API_VERSION >= 3
  175.     int i, len = strlen(string);
  176.  
  177.     for (i = 0; i < len; i++) {
  178.         width += glutBitmapWidth(font, string[i]);
  179.     }
  180. #else
  181.     width += strlen(string) * 9;
  182. #endif
  183.     return width;
  184. }
  185.  
  186. GLvoid
  187. renderStrokeString( void *font, char *string )
  188. {
  189.         int i;
  190.         int len = (int) strlen(string);
  191.         for (i = 0; i < len; i++) {
  192.                 glutStrokeCharacter(font, string[i]);
  193.         }
  194. }
  195.  
  196. GLvoid
  197. drawScene( GLvoid )
  198. {
  199.     char              str[16];
  200.     static GLfloat    whiteColor[] = { 1.0, 1.0, 1.0 };
  201.     static GLfloat    x = 3.0;
  202.  
  203.     glClear( GL_COLOR_BUFFER_BIT );
  204.  
  205.     XYaxes();
  206.  
  207.     /* update the x position each time we're called;
  208.      * wrap when we go off the screen */
  209.     x -= 0.02;
  210.     if ( x <= left )
  211.         x = right ;
  212.  
  213.     glColor3fv( whiteColor );    
  214.  
  215.     glPushMatrix();
  216.         /* Bitmap characters ARE NOT affected by 
  217.          * modeling transformations 
  218.          */
  219.         glRotatef( 15.0, 0.0, 0.0, 1.0 );
  220.         glScalef( 2.0, 2.0, 2.0 );
  221.  
  222.         /* Set position for the left end of the baseline for
  223.          *  our text string */
  224.         glRasterPos2f( x, 0.0 );
  225.         renderBitmapString( fixedFont, charStr );
  226.  
  227.         /* show the baseline */
  228.         glBegin( GL_LINES );
  229.                glVertex2f( x, 0.0 );
  230.                glVertex2f( x+1.0, 0.0 );
  231.         glEnd();
  232.     glPopMatrix();
  233.  
  234.     glPushMatrix(); 
  235.         /* Stroke characters ARE affected by 
  236.          * modeling transformations 
  237.          */
  238.         glTranslatef( -1.0, 0.0, 0.0 );
  239.         glRotatef( 15.0, 0.0, 0.0, 1.0 );
  240.         glScalef(0.003, 0.003, 0.003);
  241.         sprintf( str, "x = %5.2f", x );
  242.         renderStrokeString( strokeFont, str );
  243.     glPopMatrix();
  244.  
  245.  
  246.     glutSwapBuffers();
  247. }
  248.